package com.ckzp.weixin.wxpay;


import com.ckzp.core.ConfigUtil;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by 51594 on 2019/11/21.
 */
public class MyConfig extends WXPayConfig {

    //调用的配置位置
    public int configID = 0;

    private byte[] certData;


    public MyConfig(int configID) throws Exception {
        //不是沙箱环境要要下载证书，开出来
        this.configID = configID;
        String certPath = ConfigUtil.getProperty("WX_PAYCTRLPATH_" + configID);
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public MyConfig() throws Exception {
        //不是沙箱环境要要下载证书，开出来
        String certPath = ConfigUtil.getProperty("WX_PAYCTRLPATH");
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    /**
     * 应用ID
     *
     * @return
     */
    public String getAppID() {
        return ConfigUtil.getProperty("WX_APPID");
    }

    /**
     * 商户号
     *
     * @return
     */
    public String getMchID() {
        if (configID == 0) {
            return ConfigUtil.getProperty("WX_PAYMCHID");
        } else {
            return ConfigUtil.getProperty("WX_PAYMCHID_" + configID);
        }
    }

    //支付KEY
    public String getKey() {
        if (configID == 0) {
            return ConfigUtil.getProperty("WX_PAYKEY");
        } else {
            return ConfigUtil.getProperty("WX_PAYKEY_" + configID);
        }
    }


    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }


    public int getHttpConnectTimeoutMs() {
        return 8000;
    }


    public int getHttpReadTimeoutMs() {
        return 10000;
    }


    @Override
    IWXPayDomain getWXPayDomain() {
        return WXPayDomainImpl.instance();
    }
}